<?php
/**
 * EcataloguE
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage ecatalogue
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/EcataloguE
 */

include_spip('inc/meta');
include_spip('base/create');
function ecatalogue_upgrade($nom_meta_base_version, $version_cible){
    
	$current_version = "0.0";
    // If plugin is already installed, get it's base version
	if (isset($GLOBALS['meta'][$nom_meta_base_version])) {
		$current_version = $GLOBALS['meta'][$nom_meta_base_version];
		//spip_log('12:14 Version actuelle : '.$current_version,'ecatalogue');
	}
	
    // If plugin's base version is 0.0 (it is not installed) create database
	if ($current_version=="0.0") {
		creer_base();
        
        // Write new version to sql cache. 
        //$current_version became latest, therefore all next version_compare checks won't be executed
		ecrire_meta($nom_meta_base_version, $current_version=$version_cible);
		//spip_log('Base de donnйes ecatalogue correctement peuplйe','ecatalogue');
	}
    
    // If plugin's base version is < 0.1, alter some tables
    if (version_compare($current_version,"0.1.0","<")) {
        // Upgrade via sql_alter
        //sql_alter("TABLE spip_ecatalogue_products ADD COLUMN id_equipment bigint(21) NOT NULL");
        
        // Upgrade via maj_tables (it tells SPIP to refresh some tables)
        maj_tables('spip_ecatalogue_products');
        
        // Write new version to sql cache
        ecrire_meta($nom_meta_base_version, $current_version="0.1");
    }
    
    if (version_compare($current_version,"0.2.0","<")) {        
        //maj_tables('spip_ecatalogue_articles_similars');
        creer_base();
        maj_tables('spip_ecatalogue_products');
        ecrire_meta($nom_meta_base_version, $current_version="0.2");
    }
    
    if (version_compare($current_version,"0.3.0","<")) {        
        // Create tables that wasn't created yet
        creer_base();
        ecrire_meta($nom_meta_base_version, $current_version="0.3");
    }
    
    if (version_compare($current_version,"0.4.0","<")) {        
        // Create tables that wasn't created yet
        //creer_base();
        maj_tables('spip_ecatalogue_products');
        ecrire_meta($nom_meta_base_version, $current_version="0.4");
    }
    
    if (version_compare($current_version,"0.5.0","<")) {        
        // Create tables that wasn't created yet
        creer_base();        
        ecrire_meta($nom_meta_base_version, $current_version="0.5");
    }
    
    include_spip('base/ecatalogue_db_populate');
}


function ecatalogue_vider_tables($nom_meta_base_version) {
	//spip_log('Plugin ecatalogue correctement dйsinstallй.','ecatalogue');
    /*
    sql_drop_table('spip_ecatalogue_products');
    sql_drop_table('spip_ecatalogue_options');
    sql_drop_table('spip_ecatalogue_groupes');
    sql_drop_table('spip_ecatalogue_articles_options');
    sql_drop_table('spip_ecatalogue_currencies');
    sql_drop_table('spip_ecatalogue_rubriques_currencies');
    sql_drop_table('spip_ecatalogue_articles_similars');
    sql_drop_table('spip_ecatalogue_articles_accessoires');
    sql_drop_table('spip_ecatalogue_composites_articles');
    */  
	effacer_meta($nom_meta_base_version);
}

?>